<?php
$objXajax->registerFunction("fmfrmreporttime_load");
$objXajax->registerFunction("fmfrmreporttime_loadRoom");

function fmfrmreporttime_load($dateFrom,$dateTo,$reporttype=1){     

    global $lang;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrData = fmfrmreporttime_createArray($dateFrom,$dateTo,$reporttype);
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/customer/fm_reporttime_grid'.$reporttype.'.html');
    $oTbs->MergeBlock('blk_data', $arrData); 
    $oTbs->Show(TBS_NOTHING);  
    $objResponse->addAssign('fmfrmreporttime_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('fmfrmreporttime_tbl',true);");  
    $objResponse->addScript('changeWaiting(1);');                     
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function fmfrmreporttime_createArray($dateFrom,$dateTo,$reporttype=1){
    global $objDbUpdate,$tbl_postfix,$hotel_id;
    $sqlGroup='';
    $sql = '';
    switch($reporttype){
        case 1:  $sql = "SELECT * FROM fm_summary WHERE datediff('$dateFrom',date_sum)<=0 and datediff('$dateTo',date_sum)>=0 and service_id = '$hotel_id' order by date_sum ";
        break;
        case 2: $sql = "SELECT DATE_FORMAT(date_sum, '%u') as woy,date_add(date_sum,interval -(DATE_FORMAT(date_sum, '%w ')-1) day) as startweek,
        date_add(date_sum,interval (7-DATE_FORMAT(date_sum, '%w ')) day) as endweek,sum(money_total) as money_total,
        sum(timerent) as timerent,sum(timerent) as timerent  ,sum(numrent) as numrent,date_sum 
        FROM fm_summary WHERE datediff('$dateFrom',date_sum)<=0 and datediff('$dateTo',date_sum)>=0 and service_id = '$hotel_id' group by DATE_FORMAT(date_sum, '%u') ";  
        break;      
        case 3: $sql = "  SELECT DATE_FORMAT(date_sum, '%m') as moy,sum(money_total) as money_total,
         sum(timerent) as timerent,sum(timerent) as timerent ,sum(numrent) as numrent,date_sum 
        FROM fm_summary WHERE datediff('$dateFrom',date_sum)<=0 and datediff('$dateTo',date_sum)>=0 and service_id = '$hotel_id' group by DATE_FORMAT(date_sum, '%m')"; 
    break;
    }
    $arr = array();
    try{
        $arr = $objDbUpdate->GetArray($sql);
        $count =  count($arr);
        switch($reporttype){
            case 1 :
            case 2 :
                 for($i=0;$i<$count;$i++){
                $arr[$i]['timerent']= ConvertTimeToHM($arr[$i]['timerent']) ;
                }
            break;
        }
        
    }
    catch(Exception $e){
        $arr = array();
    } 
   // echo $sql; die(); 
    return  $arr  ;
} 
function fmfrmreporttime_loadRoom(){
    global $objDbUpdate,$strOption,$hotel_id;
    $strOption = createCombobox("fm_room",array('name','id'),array('name'=>'name','value'=>'id'),'fmfrmreporttime_cbbroom',true,'WHERE service_id='.$hotel_id); 
    $objResponse = new xajaxResponse();    
    $objResponse->addAssign('fmfrmreporttime_tdcbb', 'innerHTML', $strOption);
    $objResponse->addScript('fmfrmreporttime_createReport();');
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
} 

# end region process data
?>
